## Make PSRM Figures 1, Map of Los Angeles
## Andrew Reeves and Ryan T. Moore
## First: 27 January 2017
## Last: 19 January 2020

## NOTE: This file contains code used to create maps from individual
##  geolocations.  For privacy reasons, we have only included census
##  block lat / lon, which we have jittered.

## Load required libraries
library(maps)
library(scales)
library(sp)

## Load data:
load("figure1-LAMap.Rdata")

##### Figure 1, Los Angeles Map #####
opLocsLAbbox <- SpatialPoints(opLocsLA[,c("lonAdj", "latAdj")])
opLocsLAbbox <- bbox(opLocsLAbbox) 

png(file = "Figure-1.png")
map('county', 'california,los angeles', 
    names = TRUE, 
    plot = TRUE, 
    main = "Los Angeles, CA",
    xlim = opLocsLAbbox[1,],
    ylim = opLocsLAbbox[2,],
    type = "n",
    )

    points(opLocsLA$lonAdj[opLocsLA$personID=="Person 1"], opLocsLA$latAdj[opLocsLA$personID=="Person 1"], 
       col = alpha("red", .05),
       pch = 19, cex = .25)
    points(opLocsLA$lonAdj[opLocsLA$personID=="Person 2"], opLocsLA$latAdj[opLocsLA$personID=="Person 2"], 
           col = alpha("blue", .05),
           pch = 19, cex = .25)
    
dev.off()

## The map in the paper is generated using the following code, but it relies
## on some tempermental installations (e.g., java) so we provide the simmpler 
## code above
# library(scales)
# library(maps)
# require(OpenStreetMap)
# library(rgdal)
# library(ggplot2)
# laMap <- openproj(openmap(c(opLocsLAbbox[4] + .05, opLocsLAbbox[1] - .05),
#                  c(opLocsLAbbox[2] - .05, opLocsLAbbox[3] + .05),
#                  type = "osm-bbike"))
# 
# laLatLonMap <- autoplot(laMap) + 
#   geom_point(data = opLocsLA,
#   aes(x = lon, y = lat, shape = uid, colour = uid),
#   alpha = 1, size = 4) +
#   scale_shape_manual(values = c(1,2)) +
#   scale_colour_manual(values=(gray.colors(4))) +
#   xlab("") +
#   ylab("") +
#   theme(
#     legend.position = "none",
#     axis.ticks = element_blank(),
#     axis.text.y = element_blank(),
#     axis.text.x = element_blank(),
#     # plot.background = element_blank(),
#     panel.grid.major = element_blank(),
#     panel.grid.minor = element_blank(),
#     panel.border = element_blank(),
#     plot.background = element_rect(fill = 'white', colour = 'white')
#   )
## Figure 1, Los Angeles area map:
## png(file = "laMap.png")  
# laLatLonMap
## dev.off()
